// import { ref } from "vue"
// ​import { getLatestVersion as getVersion} from "@/api/index.js"
console.log(22222222222)
class Upgrade {
  constructor() {
    this.downloadUrl = 'https:XXXXX.apk'
    this.progress = 0
  }

   getLatestVersion(){
    const _this = this;
    return new Promise(async(resolve, reject) => {
        // getVersion().then(res=>{

        // })
        await uni.request({
            // url: 'https://haciend-aassets-5gdqxjj2b8cudbaseapp.com/app-assets/hotfix/version.txt?v=' +
            // 	new Date().getTime(), //检查更新的服务器地址
            url: 'http://124.70.9.55:19000/app-version/latest-version', //检查更新的服务器地址
            method: 'GET',
            success: (res) => {
                console.log(res);
                if (res.statusCode && res.statusCode == 200) { // api错误
                    let data = res.data.data;
                    let version = data.versionCode;
                    _this.downloadUrl = data.downloadUrl;
                    console.log(_this, version)
                    resolve(version)
                }
            }
            })
        // const version = '1.0.1'
        // // const versionInfo = getVersion();
        // console.log('versionInfo=======')
        // // _this.downloadUrl= versionInfo.downloadUrl;
        // // 相关接口

        // resolve(version)
    }).catch(err => {
        console.err(err);
    })
  }

  checkVersion(){
    return new Promise(async(resolve, reject) => {
      const localVersion = await this.getLocalVersion()
      const latestVersion = await this.getLatestVersion()
      console.log('localVersion====', localVersion, latestVersion);
      resolve(localVersion === latestVersion, localVersion, latestVersion)
    }).catch(err => {
        console.err(err);
    })
  }

  updatePackage() {
    uni.showModal({
        title: '检测到有版本更新！',
        content: '请升级app到最新版本！',
        cancelText: '暂不升级',
        confirmText: '立即升级',
        success: res => {
            console.log('下载');
            if (res.confirm) {
							console.log('1111');
                this.downloadPackage()
            }
        }
    })
  }
// ​
  downloadPackage(){
    const task = plus.downloader.createDownload(this.downloadUrl, {},
    (res, status) => {
        console.log('download=============', res, status)
				uni.showModal({
				    title: '检测到有版本更新！',
				    content: JSON.stringify(status),
				    cancelText: '暂不升级',
				    confirmText: '立即升级',
				    success: res => {
				        console.log('下载');
				        if (res.confirm) {
				            this.downloadPackage()
				        }
				    }
				})
        if (status === 200) {
            // plus.runtime.install(res.filename)
        }
    })

    this.onProgress(task)

    task.start()
  }

  onProgress(task) {
    task.addEventListener('statechanged', e => {
        if (e && e.downloadedSize > 0) {
            const progress = ((e.downloadedSize / e.totalSize) * 100).toFixed(2)
            this.progress = progress
        }
    }, false)
  }
getLocalVersion(){
    return new Promise((resolve, reject) => {
        plus.runtime.getProperty(plus.runtime.appid, function(widgetInfo) {
            const version = widgetInfo.version
            resolve(version)
        })
    }).catch(err => {
        console.err(err);
    })
}


}

export default Upgrade